<?php
namespace App\Admin\Controllers;
use Common\ControllerAdBase;
use App\Admin\Models\AdminRoles;
use App\Admin\Models\AdminRule;
use App\Admin\Models\Adminusers;
use Common\Tree;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;

class RuleController extends ControllerAdBase
{

    public function initialize()
    {
        parent::initialize();
    }

    public function adminlistAction(){
        //当前页数
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $page = 1;
        }else{
            $page = (int)$this->params['page'];
        }
        //当前条数
        if(!isset($this->params['limit']) || empty($this->params['limit'])){
            $pageSize = 10;
        }else{
            $pageSize = (int)$this->params['limit'];
        }
        $adminuser = new Adminusers();
        $list = $adminuser->admin_list($this->params);
        $paginator = new PaginatorModel(
            array(
                "data" => $list,
                "limit"=> $pageSize,
                "page" => $page
            )
        );
        $list_items = $paginator->getPaginate();
        $list_items = ToolsController::paging($list_items);
        $list_items['user_id'] = $this->user_admin['id'];
        $this->ok($list_items);
    }

    public function listAction()
    {
        $rule = new AdminRule();
        $list = $rule->rule_list();
        if (isset($this->params['type']) && !empty($this->params['type'])) {
            $tree   = new Tree();
            $mytree = $tree->list2tree($list, 'id', 'parentId');
        } else {
            $tree   = new Tree();
            $mytree = $tree->array2tree($list, 'name', 'id', 'parentId');
        }

        $this->ok($mytree);
    }


    public function addAction()
    {
        $this->valite_field($this->params);
        $rule                 = new AdminRule();
        $detail['name']       = $this->params['name'];
        $detail['rule']       = $this->params['rule'];
        $detail['parentId']   = $this->params['parentId'];
        $detail['updated_at'] = date('Y-m-d H:i:s', time());
        $detail['created_at'] = date('Y-m-d H:i:s', time());
        $result               = $rule->rule_save($detail);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '修改失败');
        }
    }

    public function editAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        $this->valite_field($this->params);
        $rule   = new AdminRule();
        $detail = $rule->find_by_id($this->params['id']);
        if (empty($detail)) {
            $this->fail(-1, '改记录不存在，或已被删除');
        }
        $detail['name']       = $this->params['name'];
        $detail['rule']       = $this->params['rule'];
        $detail['parentId']   = $this->params['parentId'];
        $detail['updated_at'] = date('Y-m-d H:i:s', time());
        $result               = $rule->rule_save($detail);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '修改失败');
        }
    }

    private function valite_field($params)
    {
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '权限名称不能为空');
        }
        if (!isset($this->params['rule']) || empty($this->params['rule'])) {
            $this->fail(-1, '权限路径不能为空');
        }
        if (!isset($this->params['parentId'])) {
            $this->fail(-1, '请选择上级权限');
        }
    }

    public function delAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        $rule   = new AdminRule();
        $detail = $rule->find_by_id($this->params['id']);
        if (empty($detail)) {
            $this->fail(-1, '改记录不存在，或已被删除');
        }
        $result = $rule->findFirstById($this->params['id'])->delete();
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '修改失败');
        }
    }


    public function addversionAction()
    {
        $this->validata_filed($this->params);
        $notices_detail['iosurl']      = $this->params['iosurl'];
        $notices_detail['description'] = $this->params['description'];
        $notices_detail['iosversion']  = $this->params['iosversion'];
        $notices_detail['is_force']    = $this->params['is_force'];
        $notices_detail['platform']    = ($this->params['platform'] == 1) ? 'ios' : 'android';
        $notices_detail['title']       = $this->params['title'];
        $notices_detail['created_at']  = date('Y-m-d H:i:s', time());
        $version                       = new Version();
        $result                        = $version->version_save($notices_detail);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '添加失败');
        }
    }


    public function updateversionAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '版本id不能为空');
        }
        $this->validata_filed($this->params);
        $version        = new Version();
        $notices_detail = $version->version_detail($this->params['id']);
        if (empty($notices_detail)) {
            $this->fail(-1, '该公告不存在或以被删除');
        }
        $notices_detail['iosurl']      = $this->params['iosurl'];
        $notices_detail['description'] = $this->params['description'];
        $notices_detail['iosversion']  = $this->params['iosversion'];
        $notices_detail['is_force']    = $this->params['is_force'];
        $notices_detail['platform']    = ($this->params['platform'] == 1) ? 'ios' : 'android';
        $notices_detail['title']       = $this->params['title'];
        $notices_detail['created_at']  = date('Y-m-d H:i:s', time());
        $result                        = $version->version_save($notices_detail);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '修改失败');
        }
    }


    private function validata_filed($params)
    {
        if (empty($params['platform'])) {
            $this->fail(-1, '更新平台不能为空');
        }
        if (empty($params['iosurl'])) {
            $this->fail(-1, '下载地址不能为空');
        }
        if (empty($params['title'])) {
            $this->fail(-1, '标题不能为空');
        }
        if (empty($params['description'])) {
            $this->fail(-1, '简介不能为空');
        }
        if (empty($params['iosversion'])) {
            $this->fail(-1, '版本号不能为空');
        }
    }


    public function delversionAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '版本id不能为空');
        }
        $version        = new Version();
        $notices_detail = $version->findFirstById($this->params['id']);
        if (empty($notices_detail)) {
            $this->fail(-1, '版本不存在，或已被删除');
        }
        $result = $notices_detail->delete();
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '删除失败');
        }
    }


    public function groupAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 10;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        $role       = new AdminRoles();
        $list_items = $role->getlist($this->params);
        if (!isset($this->params['id']) && empty($this->params['id'])) {
            $paginator  = new PaginatorModel(
                array(
                    "data"  => $list_items,
                    "limit" => $pageSize,
                    "page"  => $page
                )
            );
            $list_items = $paginator->getPaginate();
        }
        $this->ok($list_items);
    }

    /**
     * 添加/修改权限组
     */
    public function editgroupAction()
    {
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '名称不能为空');
        }

        if (!isset($this->params['rules']) || empty($this->params['rules'])) {
            $this->fail(-1, '权限组不能为空');
        }

        $roles = new AdminRoles();
        $id    = isset($this->params['id']) ? $this->params['id'] : 0;
        $temp['user_id']    = $this->user_admin['id'];
        $temp['name']       = $this->params['name'];
        $temp['rules']      = implode(',', $this->params['rules']);
        $temp['b_rules']      = implode(',', $this->params['b_rules']);
        if ($id > 0) {
            $roles              = $roles->find_by_id($id);

            $temp['updated_at'] = date('Y-m-d H:i:s');

            if ($roles->save($temp)) {
                $this->ok('操作成功！');
            }
        } else {
            $temp['created_at'] = date('Y-m-d H:i:s');
            if ($roles->save($temp)) {
                $this->ok('操作成功！');
            }
        }
        $this->fail(-1, '操作失败!');
    }

    /**
     * 权限组删除
     */
    public function delgroupAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '缺少参数ID');
        }
        $roles     = new AdminRoles();
        $rolesData = $roles->findFirstById($this->params['id']);
        if ($rolesData->delete()) {
            $this->ok('操作成功！');
        } else {
            $this->fail(-1, '操作失败!');
        }
    }

    /**
     * 权限组树状结构
     */
    public function getrolesAction()
    {
        $rules = [];
        $name  = '';
        if (isset($this->params['id']) && !empty($this->params['id'])) {
            $roleDb   = new AdminRoles();
            $roleData = $roleDb->find_by_id($this->params['id']);
            $rules    = explode(',', $roleData->rules);
            $name     = $roleData->name;
        }

        $db = new AdminRule();
        $db = $db->find();

        $newList = array();
        foreach ($db->toarray() as $val) {
            $temp['id']    = $val['id'];
            $temp['label'] = $val['name'];
            $temp['pid']   = $val['parentId'];
            $newList[]     = $temp;
        }
        $tree            = new Tree();
        $list            = $tree->list2tree($newList, 'id', 'pid', 'children');
        $result['list']  = $list;
        $result['rules'] = $rules;
        $result['name']  = $name;
        $this->ok($result);
    }
}
